package com.hua.api.modules.finance.service.impl;

import com.hua.api.core.constants.Status;
import com.hua.api.modules.finance.dao.UserAccountDao;
import com.hua.api.modules.finance.domain.UserAccount;
import com.hua.api.modules.finance.domain.UserAccountHistory;
import com.hua.api.modules.finance.repository.UserAccountHistoryRepository;
import com.hua.api.modules.finance.repository.UserAccountRepository;
import com.hua.api.modules.finance.service.UserAccountService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserAccountServiceImpl implements UserAccountService {


    private final UserAccountRepository userAccountRepository;

    private final UserAccountHistoryRepository userAccountHistoryRepository;

    private final UserAccountDao userAccountDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void onUserAccountAddUseShopId(Integer shopId, Double amount, String type, String remarks) {
        log.info("UserAccountServiceImpl.onUserAccountSub 账户出账操作 shopId={},amount={},type={},remarks={}",shopId,amount,type,remarks);
        UserAccount userAccount = userAccountDao.findUserAccountUseShopId(shopId);
        if(null!=userAccount){
            useraccountAdd(userAccount,amount,type,remarks);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void onUserAccountAdd(Integer userId, Double amount,String type, String remarks) {
        log.info("UserAccountServiceImpl.onUserAccountAdd 账户入账操作 userId={},amount={},type={},remarks={}",userId,amount,type,remarks);
        UserAccount userAccount = userAccountRepository.findUserAccountByUserId(userId);
        if(userAccount != null){
            useraccountAdd(userAccount,amount,type,remarks);
        }else{
            log.warn("UserAccountServiceImpl.onUserAccountAdd 账户不存在，无法入账操作 userId={}",userId);
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void useraccountAdd( UserAccount userAccount,Double amount,String type,String remarks){
        UserAccountHistory uah = new UserAccountHistory();
        uah.setUserId(userAccount.getUserId());
        uah.setBeforeAmount(userAccount.getAmountMoney());
        uah.setAmount(amount);
        uah.setAfterAmount(userAccount.getAmountMoney()+amount);
        uah.setCreatedTime(new Date());
        uah.setType(type);
        uah.setState(Status.LOGIC_Y);
        uah.setRemarks(remarks);
        userAccountHistoryRepository.save(uah);

        userAccount.setTotalMoney(userAccount.getTotalMoney()+amount); // 总收入
        userAccount.setAmountMoney(userAccount.getAmountMoney()+amount); // 账户余额
        userAccountRepository.save(userAccount); // update
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void onUserAccountSub(Integer userId, Double amount,String type, String remarks) {
        log.info("UserAccountServiceImpl.onUserAccountSub 账户出账操作 userId={},amount={},type={},remarks={}",userId,amount,type,remarks);
        UserAccount userAccount = userAccountRepository.findUserAccountByUserId(userId);
        if(userAccount != null){
            useraccountSub(userAccount,amount,type,remarks);

        }else{
            log.warn("UserAccountServiceImpl.onUserAccountAdd 账户不存在，无法出账操作 userId={}",userId);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void onUserAccountSubUseShopId(Integer shopId, Double amount, String type, String remarks) {
        log.info("UserAccountServiceImpl.onUserAccountSub 账户出账操作 shopId={},amount={},type={},remarks={}",shopId,amount,type,remarks);
        UserAccount userAccount = userAccountDao.findUserAccountUseShopId(shopId);
        if(userAccount != null){
           useraccountSub(userAccount,amount,type,remarks);
        }else{
            log.warn("UserAccountServiceImpl.onUserAccountAdd 账户不存在，无法出账操作 shopId={}",shopId);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void useraccountSub( UserAccount userAccount,Double amount,String type,String remarks){
        UserAccountHistory uah = new UserAccountHistory();
        uah.setUserId(userAccount.getUserId());
        uah.setBeforeAmount(userAccount.getAmountMoney());
        uah.setAmount(-amount);
        uah.setAfterAmount(userAccount.getAmountMoney()-amount);
        uah.setCreatedTime(new Date());
        uah.setType(type);
        uah.setState(Status.LOGIC_Y);
        uah.setRemarks(remarks);
        userAccountHistoryRepository.save(uah);
        userAccount.setAmountMoney(userAccount.getAmountMoney()-amount); // 账户余额
        userAccountRepository.save(userAccount); // update
    }
}
